package com.zhixiang.wbl.visitor.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;

import com.zhixiang.wbl.visitor.ActivityManager.ScreenManager;
import com.zhixiang.wbl.visitor.Constant.Constant;
import com.zhixiang.wbl.visitor.R;
import com.zhixiang.wbl.visitor.bean.Tab;
import com.zhixiang.wbl.visitor.fragement.ChatFragment;
import com.zhixiang.wbl.visitor.fragement.DiscoverFragment;
import com.zhixiang.wbl.visitor.fragement.HomeFragment;
import com.zhixiang.wbl.visitor.fragement.MyFragment;
import com.zhixiang.wbl.visitor.model.login.Visitor;
import com.zhixiang.wbl.visitor.widget.FragmentTabHost;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements TabHost.OnTabChangeListener
{
    private final static String TAG="MainActivity";
    private FragmentTabHost mTabHost;
    private LayoutInflater mInflater;
    private List<Tab> mTabLists=new ArrayList<Tab>();
    //定义当前的Visitor对象
    public Visitor mVisitor;
   // private OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mInflater= LayoutInflater.from(this);
        initTab();
        initCurrentVisitor();
        //将当前Actitity压入堆栈
        ScreenManager.getScreenManager().pushActivity(this);
    }
    /*
    * 初始化当前的Visitor对象
    * */
    private void initCurrentVisitor()
    {
        mVisitor =(Visitor) getIntent().getSerializableExtra(Constant.CURRENT_VISITOR_KEY);
        Log.e(TAG, "进来了"+mVisitor.toString() );
    }
    private void initTab()
    {
        mTabHost=(FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setOnTabChangedListener(this);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        Tab tab_home=new Tab(R.string.home,R.drawable.selector_icon_home,HomeFragment.class);
        Tab tab_chat=new Tab(R.string.chat,R.drawable.selector_icon_chat,ChatFragment.class);
        Tab tab_discover=new Tab(R.string.discover,R.drawable.selector_icon_discover, DiscoverFragment.class);
        Tab tab_my=new Tab(R.string.my,R.drawable.selector_icon_my, MyFragment.class);
        mTabLists.add(tab_home);
        mTabLists.add(tab_chat);
        mTabLists.add(tab_discover);
        mTabLists.add(tab_my);

        for (Tab tab:mTabLists)
        {
            TabHost.TabSpec tabSpec=mTabHost.newTabSpec(getString(tab.getTitle()));
            tabSpec.setIndicator(getInflateTabView(tab));
            mTabHost.addTab(tabSpec, tab.getFragment(), null);
        }

        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        mTabHost.setCurrentTab(0);
    }

    View getInflateTabView(Tab tab)
    {
        View view=mInflater.inflate(R.layout.tab_indicator,null);
        ImageView img= (ImageView) view.findViewById(R.id.icon_tab);
      //  TextView tv=(TextView)view.findViewById(R.id.txt_indicator);
        img.setBackgroundResource(tab.getIcon());
     //   tv.setText(getString(tab.getTitle()));
        return view;
    }

    @Override
    public void onTabChanged(String tabId)
    {
        if ("购物车".equals(tabId))
        {

        }
    }

    @Override
    public void onBackPressed()
    {
        //super.onBackPressed();
        ScreenManager.getScreenManager().popActivity(this);
    }

}
